﻿using Dycox.ComponentModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Dycox.Windows.Forms
{
    public class DataColumnHeader : ColumnHeader
    {
        public DataColumnHeader()
        {

        }

        public DataColumnHeader(string text, string dataMember)
        {
            this.Text = text;
            this.DataMember = dataMember;
        }

        public DataColumnHeader(string text, string dataMember, int width)
            : this(text, dataMember)
        {
            this.Width = width;
        }

        public DataColumnHeader(ViewField field)
        {
            this.Text = field.Label;
            if (field.Size > 0) this.Width = field.Size;
            else this.Width = 80;
            _dataDisplayFormat = field.Format;
            _nullText = field.NullText;
            _dataMember = field.PropertyName;
            _property = field.Property;
            _hideDefaultValue = field.HideDefaultValue;

            this.TextAlign = (HorizontalAlignment)((3 - (int)field.HAlign) % 3 );
        }

        PropertyDescriptor _property;

        private string _dataMember;

        public string DataMember
        {
            get { return _dataMember; }
            set { _dataMember = value; }
        }

        private string _dataDisplayFormat;

        public string DataDisplayFormat
        {
            get { return _dataDisplayFormat; }
            set { _dataDisplayFormat = value; }
        }

        private bool _hideDefaultValue;

        public bool HideDefaultValue
        {
            get { return _hideDefaultValue; }
            set { _hideDefaultValue = value; }
        }

        private string _nullText;

        public string NullText
        {
            get { return _nullText; }
            set { _nullText = value; }
        }


        internal protected object GetValue(object dataItem)
        {
            if (dataItem == null)
                throw new ArgumentNullException("dataItem");
            if (string.IsNullOrWhiteSpace(_dataMember))
                return null;

            return Dycox.Utils.DataBinderUtil.Eval(dataItem, _dataMember);
        }

        internal protected virtual string GetValueString(object dataItem)
        {
            object value = GetValue(dataItem);
            string text;

            if (_hideDefaultValue && _property != null)
            {
                var dva = (DefaultValueAttribute)_property.Attributes[typeof(DefaultValueAttribute)];

                if (dva != null && object.Equals(dva.Value, value))
                    return string.Empty;
            }

            if (value != null)
            {
                string format = _dataDisplayFormat;
                if (!string.IsNullOrEmpty(format))
                {
                    if (!format.Contains("{0:"))
                        format = "{0:" + format + "}";
                    text = string.Format(format, value);
                }
                else
                    text = Dycox.Utils.Formatting.ToString(value, _property);
            }
            else
                text = _nullText;

            return text;

        }
    }

}
